/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package qgj;

import java.awt.AWTEvent;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
import qgj.lib.DateChooser;
import qgj.lib.StockStruct;
import qgj.lib.Utils;

/**
 *
 * @author gluo
 */
public class EditStock extends javax.swing.JDialog {
    
    private boolean mWorkMode;
    private boolean mDirty; // emuate dirty bit
    
    private DateChooser mp;

    /**
     * Creates new form EditStock.
     * @param parent
     * @param modal
     * @param work_mode false: New, true: Edit.
     */
    public EditStock(java.awt.Frame parent, boolean modal, boolean work_mode) {
        super(parent, modal);
        initComponents();
        mWorkMode = work_mode;
        this.setResizable(false);
        CfgFrame cf = (CfgFrame) parent;
        StockStruct ss = cf.mStockForEdit;
        // load txt fields
        // Edit mode, load all the stuff
        jTxtAlertCount.setText(ss.cfg_alert_count);
        if (ss.cfg_alert_enable.equals("0"))
            jChkEnable.setSelected(false);
        else
            jChkEnable.setSelected(true);
        jTxtAlertHi.setText(ss.cfg_alert_hi);
        jTxtAlertLo.setText(ss.cfg_alert_lo);
        jTxtMsgHi.setText(ss.cfg_alert_msg_hi);
        jTxtMsgLo.setText(ss.cfg_alert_msg_lo);
        jTxtCode.setText(ss.cfg_code);
        jTxtAtDate.setText(ss.cfg_at_date);
        jTxtAtPrice.setText(ss.cfg_at_price);
        jTxtAtAmount.setText(ss.cfg_at_amount);
        jChkHold.setSelected(!ss.cfg_hold.equals("0"));
        ss.cfg_code = null; // must clear to notify cfg frame
        
        // for default enter key
        Toolkit.getDefaultToolkit().addAWTEventListener(
                new AWTEventListener()
                {
                    @Override
                    public void eventDispatched(AWTEvent e) {
                        if (e.getID() == KeyEvent.KEY_PRESSED) {
                            KeyEvent evt = (KeyEvent) e;
                            if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
                                jBtnSaveActionPerformed(null);
                            }
                        }
                    }
                }, AWTEvent.KEY_EVENT_MASK);
        
        mp = new DateChooser("yyyy-MM-dd", jTxtAtDate);
        
        //jTxtAtDate.setText(Utils.get_stime("yyyy-MM-dd"));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jTxtCode = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTxtAlertHi = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTxtAlertLo = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTxtMsgHi = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTxtMsgLo = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jTxtAlertCount = new javax.swing.JTextField();
        jTxtAtPrice = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jTxtAtDate = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jTxtAtAmount = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jChkHold = new javax.swing.JCheckBox();
        jChkEnable = new javax.swing.JCheckBox();
        jBtnCancel = new javax.swing.JButton();
        jBtnSave = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Stock Configs", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, java.awt.Color.gray));

        jTxtCode.setEditable(false);

        jLabel1.setText("Code:");

        jLabel2.setText("Alert (HI):");

        jLabel3.setText("Alert (LO):");

        jLabel4.setText("Alert Message HI:");

        jLabel5.setText("Alert Message LO:");

        jLabel6.setText("Alert Count:");

        jLabel7.setText("At Price:");

        jTxtAtDate.setEditable(false);
        jTxtAtDate.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTxtAtDateMouseClicked(evt);
            }
        });

        jLabel8.setText("At Date:");

        jLabel9.setText("At Amount:");

        jChkHold.setText("Is On Hold");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel1)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8)
                            .addComponent(jLabel6)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel9))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jTxtAtDate)
                            .addComponent(jTxtAtPrice, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTxtAlertHi, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTxtAlertLo, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTxtMsgHi, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTxtAlertCount, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTxtMsgLo, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTxtCode, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTxtAtAmount)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jChkHold)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addGap(20, 20, 20))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTxtCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTxtAlertHi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTxtAlertLo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTxtMsgHi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTxtMsgLo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jTxtAlertCount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jTxtAtPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jTxtAtDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jTxtAtAmount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jChkHold)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jChkEnable.setText("Enable Alert");

        jBtnCancel.setText("Cancel");
        jBtnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnCancelActionPerformed(evt);
            }
        });

        jBtnSave.setText("Save");
        jBtnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnSaveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jChkEnable, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(141, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jBtnSave)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jBtnCancel)
                        .addGap(42, 42, 42))))
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jChkEnable)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBtnSave)
                    .addComponent(jBtnCancel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBtnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnCancelActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jBtnCancelActionPerformed

    private void jBtnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnSaveActionPerformed
        // TODO: Adding validate check of the inputs here!!!!!
        if (!Utils.verify_code(jTxtCode.getText())) {
            JOptionPane.showMessageDialog(this, "invalid code.", "error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        CfgFrame cf = (CfgFrame)this.getParent();
        StockStruct ss = cf.mStockForEdit;

        ss.cfg_alert_count = jTxtAlertCount.getText();
        if (jChkEnable.isSelected())
            ss.cfg_alert_enable = "1";
        else
            ss.cfg_alert_enable = "0";
        ss.cfg_alert_hi = jTxtAlertHi.getText();
        ss.cfg_alert_lo = jTxtAlertLo.getText();
        ss.cfg_alert_msg_hi = jTxtMsgHi.getText();
        ss.cfg_alert_msg_lo = jTxtMsgLo.getText();
        ss.cfg_code = jTxtCode.getText();
        ss.cfg_at_date = jTxtAtDate.getText();
        ss.cfg_at_price = jTxtAtPrice.getText();
        ss.cfg_at_amount = jTxtAtAmount.getText();
        if (jChkHold.isSelected())
            ss.cfg_hold = "1";
        else
            ss.cfg_hold = "0";
        this.dispose();
    }//GEN-LAST:event_jBtnSaveActionPerformed

    private void jTxtAtDateMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTxtAtDateMouseClicked
        // TODO add your handling code here:
        mp.showPanel(this);
    }//GEN-LAST:event_jTxtAtDateMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
//        /*
//         * Set the Nimbus look and feel
//         */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /*
//         * If Nimbus (introduced in Java SE 6) is not available, stay with the
//         * default look and feel. For details see
//         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(EditStock.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(EditStock.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(EditStock.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(EditStock.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /*
//         * Create and display the dialog
//         */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            @Override
//            public void run() {
//                EditStock dialog = new EditStock(new javax.swing.JFrame(), true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//
//                    @Override
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnCancel;
    private javax.swing.JButton jBtnSave;
    private javax.swing.JCheckBox jChkEnable;
    private javax.swing.JCheckBox jChkHold;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTxtAlertCount;
    private javax.swing.JTextField jTxtAlertHi;
    private javax.swing.JTextField jTxtAlertLo;
    private javax.swing.JTextField jTxtAtAmount;
    private javax.swing.JTextField jTxtAtDate;
    private javax.swing.JTextField jTxtAtPrice;
    private javax.swing.JTextField jTxtCode;
    private javax.swing.JTextField jTxtMsgHi;
    private javax.swing.JTextField jTxtMsgLo;
    // End of variables declaration//GEN-END:variables
}
